package com.nineclock.system.controller;

import com.nineclock.api.system.OrganizationControllerApi;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/organization")
public class OrganizationController implements OrganizationControllerApi {

    @Autowired
    private SysCompanyUserService companyUserService;

    @Autowired
    private SysDepartmentService departmentService;

    /**
     * 根据当前登录用户, 获取员工列表
     *
     * @return
     */
    @Override
    @GetMapping("/members/simple")
    public Result<List<SysCompanyUserDTO>> queryCompanyMemberList() {
        List<SysCompanyUserDTO> companyUserDTOS = companyUserService.queryCompanyMemberList();
        return Result.success(companyUserDTOS);
    }

    @Override
    @GetMapping("/department")
    public Result<List<SysDepartmentDTO>> queryDepartments() {
        List<SysDepartmentDTO> departmentList = departmentService.queryDepartments();
        return Result.success(departmentList);
    }

    /**
     * @param page
     * @param pageSize
     * @param keyword
     * @param departmentId
     * @return
     */
    @Override
    @GetMapping("/members")
    public Result<PageResult<SysCompanyUserDTO>> queryMembers(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "keyword", required = false) String keyword, //可以不传递参数
            @RequestParam(value = "departmentId", defaultValue = "0") Long departmentId) {

        PageResult<SysCompanyUserDTO> pageResult = companyUserService.queryMembers(page, pageSize, keyword, departmentId);
        return Result.success(pageResult);

    }

    /**
     * 直接导入企业员工
     *
     * @param excelFile
     * @return
     * @throws IOException
     */
    @Override
    @PostMapping("/uploadExcel")
    public Result importData(MultipartFile excelFile) throws IOException {
        companyUserService.importData(excelFile);
        return Result.success();
    }

    /**
     * 查询当前企业所有部门ID集合
     *
     * @return
     */
    @GetMapping("/department/top")
    public Result<List<Long>> queryDepartmentIds() {
        List<Long> depIds = departmentService.queryDepartmentIds();
        return Result.success(depIds);
    }

    /**
     * 根据部门ID, 查询该部门下的员工数量
     *
     * @param departmentIds
     * @return
     */
    @GetMapping("/department/membernum")
    public Result<Integer> queryUserCountByDepartmentIds(@RequestParam("departmentIds") List<Long> departmentIds) {
        Integer count = companyUserService.queryUserCountByDepartmentIds(departmentIds);
        return Result.success(count);
    }

    /**
     * 根据部门ID查询子部门及员工列表
     *
     * @param departmentId
     * @param includeMember
     * @return
     */
    @Override
    @GetMapping("/simple")
    public Result<List<DepartmentOrUserSimpleDTO>> querySimleDepartment(
            @RequestParam(value = "id") Long departmentId,
            @RequestParam(value = "includeMember", defaultValue = "0") Integer includeMember) {

        List<DepartmentOrUserSimpleDTO> simpleList = departmentService.querySimleDepartment(departmentId, includeMember);

        return Result.success(simpleList);
    }

    /**
     * 根据员工id获得部门从低到高级别的部门ID数组
     * @param
     * @return
     */
    @GetMapping("/allDepId/{companyUserId}")
    public Result<List<Long>> queryDepartmentsByUserId(@PathVariable("companyUserId") Long companyUserId) {
        List<Long> depIds = departmentService.queryDepartmentsByUserId(companyUserId);
        return Result.success(depIds);
    }
}